package com.victor.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Address;
import com.victor.service.AddressService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/address")
public class AddressConrtroller {
	@Autowired
	private AddressService addressService;
	
	/**
	 * 添加地址
	 * @param address
	 * @return
	 */
	@RequestMapping("/add")
	public Msg addAddress(Address address) {
		if (address == null || address.getUid() == null || address.getUid().isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = addressService.insert(address);
		if (!b) {
			return Msg.fail("添加地址错误");
		}
		return Msg.success();
		
	}
	
	/**
	 * 修改地址
	 * @param address
	 * @return
	 */
	@RequestMapping("/updateaddress")
	public Msg updateAddress(Address address) {
		if (address == null || address.getId()==null) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.update(address);
		if (!b) {
			return Msg.fail("修改失败");
		}
		return Msg.success();
	}
	
	/**
	 * 地址详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg findXiangqing(Integer id) {
		if (id == null) {
			return Msg.fail("入参失败");
		}
		Address address = addressService.selById(id);
		return Msg.success().add("address", address);
	}
	/**
	 * 查询全部地址
	 * @param page
	 * @param row
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg findAll(int page,int row,String uid) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Address> all = addressService.selAll(page,row,uid);
		return Msg.success().add("all", all);
	}
	/**
	 *设为 默认地址
	 * @param id
	 * @param uid
	 * @return
	 */
	@RequestMapping("/moren")
	public Msg morenAddress(Integer id,String uid) {
		if (id == null || uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.updMoren(id,uid);
		if (!b) {
			return Msg.fail("修改默认地址失败");
		}
		return Msg.success();
	}
	/**
	 * 删除地址
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public Msg delete(Integer id) {
		if (id == null ) {
			return Msg.fail("入参失败");
		}
		boolean b = addressService.delete(id);
		if (!b) {
			return Msg.fail("删除失败");
		}
		return Msg.success();
	}
	/**
	 * 查询默认地址
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getmoren")
	public Msg getMoren(String uid) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		Address moren = addressService.selMoren(uid);
		return Msg.success().add("address", moren);
	}

}
